<?php
namespace app\admin\controller;

use app\admin\model\Webconfig as WebModel;
use Db;
use Request;

/**
 * 用户分信息
 */
class Webconfig extends Base
{

    public function index() //站点配制

    {
        $data = WebModel::where('groupid', 1)->select();
        $this->assign('data', $data);
        return $this->fetch();
    }

    public function add()
    {
        if ($this->request->isPost()) {
            $post = $this->request->post();

            $result = $this->validate($post, 'app\admin\validate\Webconfig');
            if (true !== $result) {
                return $this->error($result);
            }

            $data = WebModel::create($post);

            if (!empty($data) && self::fwrite()) {
                return $this->success('添加成功', 'index');
            } else {
                return $this->error('添加失败,检查目录写入权限');
            }
        }
        return $this->fetch('');
    }

    public function edit($action = '')
    {
        if ($this->request->isPost()) {
            $post = $this->request->post();
            foreach ($post as $key => $value) {
                $sk     = explode('-', $key);
                $list[] = array($sk[0] => $sk[1], 'value' => $value);
            }
            $WebModel = new WebModel;
            $data     = $WebModel->saveAll($list);
            if (!empty($data) && self::fwrite()) {
                $action = ($action) ? 'admin/Webconfig/' . $action : 'admin/Webconfig/index';
                return $this->success('修改成功', $action);
            }
        }
    }

    public function editname($gid = '')
    {
        $where = !empty($gid) ? ['groupid', '=', $gid] : "";
        $data  = WebModel::where($where)->order('groupid,sort ASC')->select();
        $this->assign([
            'web' => $data,
        ]);
        return $this->fetch('');
    }

    /**
     * 快捷操作
     * @return [type] [description]
     */
    public function quickedit()
    {
        if ($this->request->isPost()) {
            if (input('?post.name')) {
                $data   = ['id' => Request::post('id/d'), 'name' => Request::post('name')]; //状态
            } else if (input('?post.value')) {
                $data   = ['id' => Request::post('id/d'), 'value' => Request::post('value')]; //菜单
            } else if (input('?post.info')) {
                $data   = ['id' => Request::post('id/d'), 'info' => Request::post('info')]; //菜单
            } else if (input('?post.sort')) {
                $data   = ['id' => Request::post('id/d'), 'sort' => Request::post('sort/d')]; //菜单
            } else if (input('?post.groupid')) {
                $data   = ['id' => Request::post('id/d'), 'groupid' => Request::post('groupid/d')]; //菜单
            } else if (input('?post.varname')) {
                $data   = ['id' => Request::post('id/d'), 'varname' => Request::post('varname/d')]; //菜单
            }
            $model = WebModel::update($data);
            if (!empty($model) && self::fwrite()) {
                return $this->success('修改成功');
            } else {
                return $this->error('修改失败,检查目录写入权限');
            }
        }
    }

    /**
     * 上传附件配制
     * @return [type] [description]
     */
    public function upconfig()
    {
        $data = WebModel::where('groupid', 2)->order('sort ASC')->select();
        $this->assign('data', $data);
        return $this->fetch('index');
    }

    /**
     * 系统配制
     * @return [type] [description]
     */
    public function system()
    {
        $data = WebModel::where('groupid', 3)->order('sort ASC')->select();
        $this->assign('data', $data);
        return $this->fetch('index');
    }

    /**
     * 短信配制
     * @return [type] [description]
     */
    public function msg()
    {
        $data = WebModel::where('groupid', 4)->order('sort ASC')->select();
        $this->assign('data', $data);
        return $this->fetch('index');
    }

    /**
     * 上传附件配制
     * @return [type] [description]
     */
    public function web()
    {
        $data = WebModel::where('groupid', 1)->order('sort ASC')->select();
        $this->assign('data', $data);
        return $this->fetch('index');
    }
    /**
     * 将配制文件写入文本减少查询
     * @return [type] [description]
     */
    private static function fwrite()
    {
        $weburl     = "../config/webconfig.php";
        $settingstr = "<?php \n\n //站点配制文件,为系统自动生成\n\n\n return [\n";
        $webconfig  = Db::name('webconfig')->order('groupid,sort ASC')->select();
        foreach ($webconfig as $k => $v) {

            $settingstr .= "\t\n'" . $v['varname'] . "'=>'" . $v['value'] . "', //" . $v['info'] . " \n";
        }
        $settingstr .= "\n];\n\n";

        if (file_put_contents($weburl, $settingstr)) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * 图片上传过程
     * @return [type] [description]
     */
    public function upload($file = '')
    {
        $webinfo['path'] = config('webconfig.upload_path');
        $webinfo['path'] = !empty($file) ? ($webinfo['path'] . '/' . $file) : $webinfo['path'];
        $webinfo['path'] = $webinfo['path'] .'/'.date('y-m-d',time());
        $webinfo['size'] = config('webconfig.file_size');
        // 获取表单上传文件 例如上传了001.jpg
        $file = request()->file('file');
        // 移动到框架应用根目录/uploads/ 目录下
        $info = $file->validate(['size' => $webinfo['size'], 'ext' => 'jpg,png,gif'])->rule('date')->move('../public/' . $webinfo['path'],uniqid());
        if ($info) {
            // 成功上传后 获取上传信息
            $data = [
                'code' => '0',
                'msg'  => '上传成功',
                'data' => [
                    'src'   => '/' . $webinfo['path'] . '/' . $info->getSaveName(),
                    'title' => $info->getFilename(),
                ],
            ];
        } else {
            // 上传失败获取错误信息
            $data = [
                'code' => '1',
                'msg'  => $file->getError(),
                'data' => [],
            ];
        }
        return json($data);
    }

}
